<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 供应商商品管理控制器
 */

class GoodsController extends Controller{
	/**
	 * 商品列表
	 */
	public function index(){
		$where['supptypeid']=$_SESSION['supperid'];
		///$where['supptypeid']=1;
		$page=I('get.page','1','strip_tags');
        $count=M('item')->where($where)->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
		$result=M('item')->where($where)
		                 ->limit($pagearr['start'],$pagearr['pagesize'])
		                 ->select();
		      
		$this->assign('items',$result);
		$this->assign('pagearr',$pagearr);
		$this->display('index');	



 

	}

	/**
	 * 查看商品详情
	 */
	public function getItem(){
		$itemid=I('get.itemid','','strip_tags');
      if(!$itemid) return false;
      //获取分类树
     // $type=new \Admin\Controller\TypeController();
     // $type=A('Daili://Admin/TypeController');
	  $typearr=$this->getListCat();
      //获取商品基本信息
      $item=M('item');
      $iteminfo=$item->where('itemid=%d',$itemid)->select();
      //获取该类目下的所有属性
		$typeproperty=M('type a')->join('inner join '. C('DB_PREFIX').'type_property b on a.typeid=b.typeid ')
		          ->where('a.typeid=%d',$iteminfo[0]['typeid'])
		          ->getField('b.propertyid,a.typeid,a.typename,b.propertyname');
		  if(!empty($typeproperty))
		  {
		  	foreach($typeproperty as $key=>&$value)
		  	{
		 	$where['propertyid']=$key;
		 	$value['select']=M('type_property a')->join('inner join '.C('DB_PREFIX').'type_propertyvalue b on a.propertyid=b.propertyid')
		 		             ->where('a.propertyid=%d',$where['propertyid'])
		 		             ->getField('b.valueid,a.propertyid,a.propertyname,b.propertyvalue ');
		 	}

		  }  
		//获取已选择的属性
		$seledpro=M('item_property')->where('itemid=%d',$iteminfo[0]['itemid'])->getField('propertyid,propertyvalueid');
		 foreach($typeproperty as $key=>&$value)
		 {
             
		 	foreach($value['select'] as $k=>&$v)
		 	{
		 		
		 		if($v['valueid']==$seledpro[$key])
		 		{

		 			$v['selected']='true';
		 		}
		 	}
		 }
		//获取该类目下所有品牌
		 $brands=M('type_brand a ')->join('inner join '.C('DB_PREFIX').'brand b on a.brandid= b.brandid')
		         ->where('b.isdel=0 AND a.typeid='.$iteminfo[0]['typeid'])
		         ->getField('b.brandid,b.brandname');
		 
		 //获取该类目下所有规格
		 $spec_type=M('type_spec a ')->join('inner join '.C('DB_PREFIX').'spec b  on a.specid =b.specid ')
		  			->where('a.typeid=%d',$iteminfo[0]['typeid'])->getField('a.specid,a.typeid,b.`name`');
		 if(!empty($spec_type))
		 {
		 	foreach($spec_type as $key=>&$value)
		 	{
		 			$where['specid']=$key;
		 			$value['specval']=M('specval')->where('specid=%d',$where['specid'])->getField('specvalid,specid,name');
		 	}
		 }
		 //该产品选中的规格
		 $serspec=M('item_spec')->where('itemid=%d',$itemid)->getField('specid,self');
		 foreach($serspec as $k=>&$v){
		 	$v=unserialize($v);
		 }
		 foreach($spec_type as $key=>&$value){
		 	foreach($value['specval'] as $k=>&$v){
		 		foreach($serspec[$key] as $i=>$sname)
		 		 {
		 		 	if($i==$v['specvalid']){
		 		 		$v['checked']='true';
		 		 	}
		 		}
		 		
		 	}
		 }
		//判断此商品是否为货品(即不同规格对应不同价格和库存)
	  $products=M('product')->where('itemid=%d',$itemid)->getField('productid,itemid,inventory,price');
	  if($products){
         $productspecs=M('product_spec')->where('itemid=%d',$itemid)->select(); 
         foreach($productspecs  as $spec){
         		$products[$spec["productid"]]["specs"][$spec["specid"]] =$spec["specvalid"];
         }  
	  }
	  //获取商品详情
	  $detail=M('item_desc')->where('itemid=%d',$itemid)->getField('itemdesc');
	  $this->assign('detail',$detail);
	  $this->assign('typearr',$typearr);
	  $this->assign('products',$products);
 	  $this->assign('iteminfo',$iteminfo[0]);
	  $this->assign('brands',$brands);
	  $this->assign('typeproperty',$typeproperty);
	  $this->assign('specs',$spec_type);
	  
      $this->display('getitem');
	}

	/**
	 *
	 * 返回已经确定子分类的类别属性，计算等级
	 *
	 */
	public function getListCat($data = array(),$level = 0) {
		$cats = array();
		!$data && $data = $this->getCats();
		if(!$data) return $cats;
		foreach($data as $key => $val) {
			$cats[$val['typeid']] = &$data[$key];
			$cats[$val['typeid']]['level'] = $level;
			if( isset( $val['children'] ) ) {
				$cats[$val['typeid']]['haschild'] = 1;
				$cats += $this->getListCat($val['children'],$level + 1);
				unset($data[$key]['children']);
			} 
		}
		return $cats;
	}
	
	/**
	 *
	 * 获取所有类别
	 *
	 */
	protected function getCats($return = 'tree') {
		static $retarr;
		if(isset($retarr[$return])) return $retarr[$return];

		$cats = M('type')->getField('typeid,typename,pid');
		if($return == 'tree')
		{			//返回数组
			$tree = array();

			foreach($cats as $cat)
			 {	//循环cat
				if(isset($cats[$cat['pid']])) 
				{//非第一级
					$cats[$cat['pid']]['children'][] = &$cats[$cat['typeid']];
				} else
				 {//第一级
					$tree[] = &$cats[$cat['typeid']];

				}
			}
			$retarr[$return] = $tree;
		} else if($return == 'source')
		 {//返回source
			$retarr[$return] = $cats;
		}
		return $retarr[$return];
	}
	

}